//
// vxe-table-extends.js
// hs-admin
//
// Created by 区区电脑 on 2023/05/10.
//
// vxe-table 扩展功能

import {ref, computed} from "vue"

export default function vxeTableExtends() {
    const VXETable = ref()

    // 选中
    const checkedRows = ref([]);
    // 不允许批量操作 （批量操作按钮 disabled）当没有选中数据的时候通过获取该属性的值来使批量操作按钮为不可点击状态
    const isCantAll = computed(() => {
        return checkedRows.value <= 0;
    })

    // 单行选中/取消
    const tableRowCheckChange = ({row, checked}) => {
        if (checked) {
            const index = checkedRows.value.findIndex(item => item.id === row.id);
            if (index < 0) {
                checkedRows.value.push(row)
            }
        } else {
            const index = checkedRows.value.findIndex(item => item.id === row.id);
            if (index > -1) {
                checkedRows.value.splice(index, 1)
            }
        }
    }

    // 全部选中/取消
    const tableRowAllCheckChange = ({checked, records}, vn) => {
        if (checked) {
            checkedRows.value = $lodash.cloneDeep(records);
        } else {
            checkedRows.value = [];
            vn?.setAllCheckboxRow(false)
        }
    }

    return {
        checkedRows,
        isCantAll,
        tableRowCheckChange,
        tableRowAllCheckChange
    }
}
